from typing import Optional
from sqlmodel import Field, SQLModel, Relationship
from sqlalchemy import event
from models.common import get_current_time
from enum import Enum


from models.Base import StatusTypeEnum

class TaskTypeEnum(str, Enum):
    SHHSCRIPT = 'shhscript'
    HTTP = 'http'

class TriggerTypeEnum(str, Enum):
    # 间隔秒
    INTERVAL = 'interval'
    # 间隔分钟
    INTERVALMINUTE = 'intervalminute'
    # 间隔小时
    INTERVALHOUR = 'intervalhour'
    # 每天
    EVERYDAY = 'everyday'
    # 每周
    EVERYWEEK = 'everyweek'
    # 每2周
    EVERY2WEEK = 'every2week'
    # 每月
    EVERYMONTH = 'everymonth'
   

class task_job(SQLModel, table=True):
    __tablename__ = "sys_task_job"  # 确保表名一致
    # 唯一索引
    id: Optional[str] = Field(default=None,max_length=100, primary_key=True)
    type: TaskTypeEnum = Field(nullable=False, title='任务类型')  # 任务类型
    name: str = Field(nullable=False,index=True, title='任务名称')  # 任务名称
    triggerType: TriggerTypeEnum = Field(nullable=False, title='触发器类型')  # 触发器类型
    month: Optional[int] = Field(default=None, title='月份')  # 月份
    day: Optional[int] = Field(default=None, title='天')  # 天
    hour: Optional[int] = Field(default=None, title='小时')  # 小时
    minute: Optional[int] = Field(default=None, title='分钟')  # 分钟
    second: Optional[int] = Field(default=None, title='秒')  # 秒
    status: StatusTypeEnum = Field(default=StatusTypeEnum.NORMAL, title='状态',description="0:失败 1:成功")  # 状态
    role: Optional[str] = Field(nullable=True,max_length=100, default=None, title='任务执行者')  # 角色 // 任务执行者
    content: Optional[str] = Field(nullable=True, default=None,  title='任务内容')   # 任务内容
    url: Optional[str] = Field(nullable=True, default=None,  title='http任务地址')  # http任务地址
    url_agent: Optional[str] = Field(nullable=True, default=None,  title='http user_agent')   # http agent
    lastRunTime: Optional[int] = Field(default=None, title='最后执行时间')  # 最后执行时间
    createtime:  Optional[int] = Field(default=get_current_time(), nullable=False,title='创建时间')
    admin_id: Optional[int] = Field(nullable=False, default=None, title='创建者')

class task_job_record(SQLModel, table=True):
    __tablename__ = "sys_task_job_record"  # 确保表名一致
    id: Optional[int] = Field(default=None, primary_key=True)
    job_id: Optional[str] = Field(nullable=False, foreign_key="sys_task_job.id")
    job_name: str = Field(nullable=False,title='任务名称')
    status: StatusTypeEnum = Field(default=StatusTypeEnum.NORMAL ,nullable=False,title='状态', index=True,description="0:失败 1:成功")
    message: str = Field(nullable=False,title='详细信息')
    createtime:  Optional[int] = Field(default=get_current_time(), nullable=False,title='时间')